<?php
                require 'db.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> muhaha </title>
<meta name="keywords" content="" />
<meta name="description" content=" mufafa" />
<link rel="stylesheet" type="text/css"  href="/css/main.css"  />
<link rel="stylesheet" type="text/css"  href="jquery.tooltip.css"  />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.tooltip.min.js"></script> 
<script src="jquery.masonry.min.js"></script>
<script type="text/javascript" src="masonry/sc.js"></script> 
<script type="text/javascript" src="masonry/jquery.infinitescroll.min.js"></script> 
</head>
        
        <body>
            <?php
                
                
                $tpl = "<img width='%spx' height='%spx' src='http://img1".$__host."/datas/%s' />";
                $sql = "";
                $currentpage = 1; // 当前页
                $fid = -1;
                $cond = '';
                $kw = '';
                $page_offset = '0';
                $catkw = '';
                if (array_key_exists( 'kw', $_GET)){
                    $kw = $_GET['kw'];
                    $cond = 'where title COLLATE UTF8_GENERAL_CI like "%'.$kw.'%"';
                }
                else if (array_key_exists( 'catkw', $_GET)){
                    $catkw = $_GET['catkw'];
                    $cond = 'where cat ="'.$catkw.'"';
                }
                if (array_key_exists( 'page', $_GET)){
                     intval($_GET['page']) or die("FUCK OFF!");
                     $sql = "SELECT code, fid, title, preview, tw, th, cat FROM post %s order by postdate desc limit %s, 20";
                     $page_offset = (intval($_GET['page'])-1)*20;
                     $sql = sprintf($sql, $cond, $page_offset);
                     $currentpage = intval($_GET['page']);
                     if (array_key_exists('fid', $_GET)){
                         if ($cond != ''){
                             $cond = str_replace('where', '', $cond) ;
                             $cond .= ' and ';
                         }
                          $fid = $_GET['fid'];
                         $sql = "SELECT code, fid, title, preview, tw, th, cat FROM post where %s fid in(%s) order by postdate desc limit %s, 20";
                         $sql = sprintf($sql, $cond, $_GET['fid'], (intval($_GET['page'])-1)*20);
                     }
                } else{
                    
                    $sql = "SELECT code, fid, title, preview, tw, th, cat FROM post  order by postdate desc limit 20";
                }
                // tag搜索
                $tagkey = '';
                if (array_key_exists('tag', $_GET)){
                   $sql = 'select code, fid, title, preview, tw, th, cat from post p where pid in '; 
                   $sql .= '(select pid from tag_rela r where r.tag_key="%s") ';
                   $sql .= 'order by p.postdate desc limit %s, 20';
                   $tagkey = $_GET['tag'];
                   $sql = sprintf($sql, $tagkey, $page_offset);
                } 
                //echo $sql;
            ?>
          
          
            
            <?php
                require 'header.php';
             ?>
             
            
            <?php
                require 'index_pager.php';
             ?>
             
          
          <div id='content'>
              <?php
                if ($result = $mysqli->multi_query($sql)) {
                    if ($result = $mysqli->use_result()) {
                        while ($row = $result->fetch_row()) {
                            $code = $row[0];
                            $fid = $row[1];
                            $title = $row[2];
                            $thumburl = $row[3];
                            $tw = $row[4];
                            $th = $row[5];
                            $cat = $row[6];
                            $ratio = 1;
                            $preview_image = sprintf($tpl,$tw*$ratio,$th*$ratio, $thumburl);
                            $full_preview = str_replace('crop_', '', $thumburl);
                            ?>
                            
                                <div class='content_item prv' prv='<?=$full_preview?>' > 
                                    <span class='catspan'>
                                        <a style='text-decoration:none;color:grey;' href="index.php?page=1&fid=<?=$fid?>&catkw=<?=$cat?>" ><?=$cat?></a>
                                    </span>
                                    <a target=_blank href='post.php?code=<?=$code?>'>
                                        <?=$preview_image?>
                                    </a>
                                <p><a target="_blank" href='post?code=<?=$code?>'>    <?=$title ?> </p></a>
                            </div>
                            
                            <?php
                        }
                    }
                
                    /* free result set */
                    $result->close();
                }
                ?>
          </div>
          
          <div id='page_down'></div>
               <nav style='position:absolute;display:none;float: right;bottom:700px;' id="page-nav">
                <?php 
                    if ($fid == -1){
                        
                    echo "<a  href=\"?page=",$currentpage+1, "\">下一页</a>";
                    
                    }else{
                        echo "<a href=\"?fid=",$fid,"&page=",$currentpage+1, "\">下一页</a>";
                        
                    }
                ?>
            </nav>
            
          <div id='footer'></div>
        
        <script>
                $(function(){

                      // cat label
                      $('.content_item').each(function(){
                          var cattext = $(this).find('.catspan').html();
                          $(this).hover(function(){
                            $(this).find('.catspan a').css('color', 'white');
                          },
                          function(){
                            $(this).find('.catspan a').css('color', 'grey');
                          })
                      })

                    var $container = $('#content');
                      $container.masonry({
                        itemSelector : '.content_item'
                        // ,isFitWidth:true
                      });
                      
                      $container.infinitescroll({
                          navSelector  : '#page-nav',    // selector for the paged navigation 
                          nextSelector : '#page-nav a',  // selector for the NEXT link (to page 2)
                          itemSelector : '.content_item',     // selector for all items you'll retrieve
                          loading: {
                              msgText  : '',
                              finishedMsg: '已经没有内容.',
                              img: 'http://www.infinite-scroll.com/wp-content/plugins/infinite-scroll/ajax-loader.gif',
                              selector:'#page_down'
                            }
                          },
                          
                          function( newElements ) {
                            var $newElems = $( newElements ).css({ opacity: 0 });
                              $newElems.animate({ opacity: 1 });
                              $container.masonry( 'appended', $newElems, true ); 
                          }
                          
                        );
                        
                        
                      
                })
                function goto(){
                    var pg=$('#topage').val();
                    window.location.href = window.location.href.replace(/page=(\d*)/,'page='+pg);
                    return false;
                }
                function addPrvList(){
                    $(".prv").tooltip({ 
                        bodyHandler: function() { 
                            return '<img src="'+$(this).attr("prv")+'">'; 
                        }, 
                        showURL: false ,
                        top : -300,
                        left: -200
                    });
                }
        </script>
          
        </body>
        
</html>
